/**
 * LCR 130. 衣橱整理
 * https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/description/
 */
class Solution {
    int[] dx = {0, 1};
    int[] dy = {1, 0};
    boolean[][] vist;
    int m, n;
    int ret = 0;
    public int wardrobeFinishing(int _m, int _n, int cnt) {
        m = _m; n = _n;
        vist = new boolean[m][n];
        dfs(0, 0, cnt);
        return ret;
    }
    public void dfs(int i, int j, int cnt) {
        ret++;
        vist[i][j] = true;
        for(int k = 0; k < 2; k++) {
            int x = i + dx[k], y = j + dy[k];
            int _x = digit(x), _y = digit(y);
            if(x >= 0 && x < m && y >= 0 && y < n && !vist[x][y] && _x + _y <= cnt) {
                dfs(x, y, cnt);
            }
        }
    }
    public int digit(int x) {
        int tmp = 0;
        while(x > 0) {
            tmp += x % 10;
            x /= 10;
        }
        return tmp;
    }
}